﻿#region Copyright 2008 - Evolution Systems.
//
// Author: José Rogério Ferreira dos Santos(joserogerio.santos@gmail.com)
// Date: 24/01/2009
// Time: 14:34
// All rights are reserved. Reproduction or transmission in whole or in part, in
// any form or by any means, eletronic, mechanical or otherwise, is prohibited
// without the prior written permission of the author.
//
// Filename: ${FILE_NAME}.cs
#endregion
using System;
using System.Collections.Generic;

namespace csharp.personal.rts
{
	/// <summary>
	/// Description of User.
	/// </summary>
	public class User : csharp.common.DefaultClass, csharp.common.IClass
	{
		#region private Declaration
		private string _real_name;
		private string _name;
		private string _pass;
		private string _email;
		private string _create_date;
		private string _active;
		#endregion
		
		public User()
		{
			gsql.table = "user";
			_sequence  = "user_user_id_"; //Gambiarra por causa do postgres
		}
		
		#region properties
		public string realName 
		{
			get { return _real_name; }
			set { _real_name = value; }
		}
		
		public string name 
		{
			get { return _name; }
			set { _name = value; }
		}
		
		public string pass 
		{
			get { return _pass; }
			set { _pass = value; }
		}
		
		public string email 
		{
			get { return _email; }
			set { _email = value; }
		}
		
		public string createDate 
		{
			get { return _create_date; }
			set { _create_date = value; }
		}
		
		public bool active 
		{
			get { return Convert.ToBoolean(_active); }
			set { _active = Convert.ToString(value); }
		}
		
		#endregion
	
		public void clear()
		{
			_id			= string.Empty;
			_real_name  = string.Empty;
			_name 		= string.Empty;
			_pass 		= string.Empty;
			_email		= string.Empty;
			_create_date= string.Empty;
			_active		= bool.TrueString;
		}
	
		public override void get(System.Data.DataRow dr)
		{
			_id			= dr["user_id"].ToString();
			_real_name	= dr["user_real_name"].ToString();
			_name			= dr["user_name"].ToString();
			_pass			= csharp.common.Util.decrypt(dr["user_pass"].ToString());
			//_pass			= dr["user_pass"].ToString();
			_email		= dr["user_email"].ToString();
			_create_date= dr["user_create_date"].ToString();
			_active		= Convert.ToBoolean(dr["user_active"]).ToString();//Gamb mysql 0, 1 = true, false
		}
		
		public override void setGsql()
		{
			//Gamb mysql
			string aux = string.Empty;
			if (Convert.ToBoolean(_active))
				aux = "1";
			else
				aux = "0";
			
			gsql.setValue("1", "user_id");
			gsql.setValue(_real_name,		"user_real_name");
			gsql.setValue(_name,				"user_name");
			gsql.setValue(csharp.common.Util.encrypt(_pass),				"user_pass");
			//gsql.setValue(_pass,				"user_pass");
			gsql.setValue(_email,			"user_email");
			//gsql.setValue(_create_date,	"user_create_date");
			gsql.setValue(aux,				"user_active");
		}
		
	}
	
	public class Users : csharp.common.DefaultList, csharp.common.IList
	{
		List<User> _list = new List<User>();
		
		public Users()
		{
			gsql.table = "user";
		}
		
		
		public User get(int index)
		{
			return _list[index];
		}
		
		
		public User get()
		{
			return _list[index];
		}
		
		
		public int count()
		{
			return _list.Count;
		}
		
		public void add(csharp.common.DefaultClass value)
		{
			_list.Add((User)value);
		}
		
		public bool fillList()
		{
			gsql.prepare();
			gsql.setValue("","*");
			
			return mountList();
		}
		
		private bool mountList()
		{
			int tam;
			
			if (!gsql.execute())
			{
				msgErro = gsql.getMsgErro();
				return false;
			}
			
			tam = gsql.getRowCount();
			for (int i=0; i < tam; i++)
			{
				User u = new User();
				u.get(gsql.getRow(i));
				
				_list.Add(u);
			}
			return true;
		}
		
		public bool search(string value)
		{
			int tam;
			
			tam= _list.Count;
			for(int i=0; i < tam; i++)
			{
				if (_list[i].id.Equals(value))
				{
					index = i;
					return true;
				}
			}
			return false;
		}
		
		public void clear()
		{
			_list.Clear();
		}
		
	}
}
